package com.mzy.olddate.demo.classpower;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2021/8/28 20:53
 * @Version 1.0
 */
public class Lunch {
    void testPrivate(){
    //
    }
    static void testStatic(){
        Soup1 soup1 = Soup1.makeSoup();
    }
    static void testSingleton(){
        //
        Soup2.access().f();
    }

    public static void main(String[] args) {
//        testSingleton();
        testStatic();
    }

}

// 快速新建多个类，节省时间
class Soup1 {
    private Soup1(){}
    public static Soup1 makeSoup(){
        int i=0;
        for ( ;i < 10; i++) {
            System.out.println("打印创建对象的次数"+i);
        }
        if (i==5){
            return makeSoup();
        }
        return new Soup1();
    }
}

class Soup2{
    // 创建一个静态对象，并且返回一个引用。这里用到了单例模式。以后有需要再了解
    private Soup2(){}
    private static Soup2 ps1=new Soup2();
    public static Soup2 access(){
        return ps1;
    }
    public void f(){
        System.out.println("打印通过单例模式，静态方法创建对象，并调用私有构造方法的公开方法");
    }
}
